package com.lifesaftypower.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

import android.content.Context;

import com.lifesaftypower.MainApp;

public class CacheManager {
	private static String _cacheRootPath;
	static {
		Context context = MainApp.getAppContext();
		_cacheRootPath = context.getCacheDir().getPath();
	}

	public static InputStream GetCachedFile(CacheType cacheType, String key) {
		File cacheDir = new File(_cacheRootPath + "/" + cacheType.toString());
		File file = new File(cacheDir + "/" + key);
		FileInputStream ins = null;
		try {
			ins = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return ins;
	}

	public static String GetCachedFilePath(CacheType cacheType, String key) {
		File cacheDir = new File(_cacheRootPath + "/" + cacheType.toString());
		return cacheDir + "/" + key;		
	}
	
	public static void PutUrlResourceIntoCache(String url, InputStream ins) {
		try {
			UrlInfo info = HttpUtil.GetUrlInfo(url);
			File cacheDir = new File(_cacheRootPath + "/" + info.get_domain() + info.get_path());
			if(!cacheDir.exists()) {
				cacheDir.mkdirs();
			}
			
			File file = new File(cacheDir + "/" + info.get_file());
			OutputStream out = null;
			try {
				out = new BufferedOutputStream(new FileOutputStream(file));
				byte[] buffer = new byte[2048];
				int size = ins.read(buffer);
				while (size > 0) {
					out.write(buffer, 0, size);
					size = ins.read(buffer);
				}

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (out != null) {
					try {
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (MalformedURLException e) {			
			e.printStackTrace();
		}
	}
	
	public static boolean HasUrlResourceInCache(String url) {
		UrlInfo info;
		try {
			info = HttpUtil.GetUrlInfo(url);
			File cacheDir = new File(_cacheRootPath + "/" + info.get_domain() + info.get_path());
			if(!cacheDir.exists()) {
				cacheDir.mkdirs();
			}
			
			File file = new File(cacheDir + "/" + info.get_file());
			return file.exists();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static InputStream GetUrlResourceFromCache(String url) {		
		try {
			UrlInfo info;
			info = HttpUtil.GetUrlInfo(url);
			File cacheDir = new File(_cacheRootPath + "/" + info.get_domain() + info.get_path());
			if(!cacheDir.exists()) {
				cacheDir.mkdirs();
			}
			
			File file = new File(cacheDir + "/" + info.get_file());
			FileInputStream ins = null;
			try {
				ins = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			return ins;
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return null;
	}
	
	public static void PutCachedFile(CacheType cacheType, String key,
			InputStream ins) {
		File cacheDir = new File(_cacheRootPath + "/" + cacheType.toString());
		if (!cacheDir.exists()) {
			cacheDir.mkdirs();
		}
		File file = new File(cacheDir + "/" + key);
		OutputStream out = null;
		try {
			out = new BufferedOutputStream(new FileOutputStream(file));
			byte[] buffer = new byte[4096];
			int size = ins.read(buffer);
			while (size > 0) {
				out.write(buffer, 0, size);
				size = ins.read(buffer);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
		
}
